package hichecker.ui.components.custom_table.table_cell_renderer;

import hichecker.controller.Config;
import hichecker.ui.components.custom_table.AbstractCustomTableModel;
import hichecker.ui.components.custom_table.column_class.Grade;
import hichecker.ui.components.custom_table.column_class.Points;
import hichecker.ui.custom_views.series.SeriesViewTableModel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * Customized cell renderer, which aligns number right and colours cells that have been changed
 */
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
	/* CONSTRUCTOR ***************************************************************************** */
	/**
	 * Creates the renderer
	 */
	public CustomTableCellRenderer() {
		super();
	}

	/* FUNCTION ******************************************************************************** */
	/**
	 * Prints the text und colours the cell depending on the cell's value and it's select state.
	 */
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
			boolean hasFocus, int tableRow, int tableColumn) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, tableRow, tableColumn);

		// Converts indixes from table to model
		int row = table.convertRowIndexToModel(tableRow);

		// Align a number right, everything else left
		Class<?> columnClass = table.getColumnClass(tableColumn);
		if (isInRightAlignment(columnClass))
			setHorizontalAlignment(JLabel.RIGHT);
		else
			setHorizontalAlignment(JLabel.LEFT);

		// Has current cell been changed?
		AbstractCustomTableModel<?> tableModel = (AbstractCustomTableModel<?>) table.getModel();
		boolean hasChanged = tableModel.cellsHaveChanged(row,
				TableModelEvent.ALL_COLUMNS);

		// Set background color
		boolean isGroup = table.getModel() instanceof SeriesViewTableModel
				&& ((SeriesViewTableModel) table.getModel()).isInGroup(tableRow);
		Color bg = null;

		if (isSelected)
			bg = isGroup ? Config.COLOR_GROUP_SELECTED : table.getSelectionBackground();
		else
			bg = isGroup ? Config.COLOR_GROUP_UNSELECTED : table.getBackground();


		// TODO: add change coloring
//		if (isSelected)
//			bg = hasChanged ? Config.COLOR_CHANGED_SELECTED : table.getSelectionBackground();
//		else
//			if (table.getModel() instanceof SeriesViewTableModel
//					&& ((SeriesViewTableModel) table.getModel()).isInGroup(tableRow))
//				bg = Config.COLOR_GROUP;
//			else
//				bg = hasChanged ? Config.COLOR_CHANGED_UNSELECTED : table.getBackground();
		setBackground(bg);

		// Border for selected, changed cells
		if (hasChanged && hasFocus && isSelected) {
			Insets i = getBorder().getBorderInsets(this);
			setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(new Color(
					253, 237, 162)), BorderFactory.createEmptyBorder(i.top, i.left, i.bottom,
					i.right)));
		}
		return this;
	}

	private boolean isInRightAlignment(Class<?> columnClass) {
		boolean result = false;
		result |= columnClass == Points.class;
		result |= columnClass == Grade.class;
		result |= columnClass == Integer.class;
		result |= columnClass == Float.class;
		return result;
	}

}